package net.xfok;

import net.xfok.http.client.HttpClient;
import net.xfok.mail.sender.GMailSender;
import net.xfok.mail.sender.JMailSender;
import net.xfok.mail.sender.MailSender;
import net.xfok.rss.parser.Feed;
import net.xfok.rss.parser.Rss;
import net.xfok.string.filter.Filter;
import net.xfok.string.filter.Rule;
import net.xfok.ui.animation.Rotate3D;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener{
	private ViewGroup container;
	private TextView tv;
	private WebView wv;
	private Animation mAnim;
	private Button bt3D,btDB,btCube,btDict;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text);
        wv=(WebView)findViewById(R.id.web);
        bt3D=(Button)findViewById(R.id.bt_3d);
        bt3D.setOnClickListener(this);
        btDB=(Button)findViewById(R.id.bt_db);
        btDB.setOnClickListener(this);
        btCube=(Button)findViewById(R.id.bt_cube);
        btCube.setOnClickListener(this);
        btDict=(Button)findViewById(R.id.bt_dict);
        btDict.setOnClickListener(this);
        container=(LinearLayout)findViewById(R.id.container);
        mAnim=new Animation(container);
//        testRssParser();
//        testJMailSender();
//        testGMailSender();
//        testHttpClient();
//        testFilter();
    }
    
    private void testRssParser(){
    	String url="http://finance.ifeng.com/rss/headnews.xml";
        Rss rss=new Rss(url);
        for(Feed feed:rss.getFeeds()){
        	Log.i("Main", feed.getLink());
        }
    }
    
    private void testJMailSender(){
    	String smtp = "smtp.sina.com"; 
    	String username = "alex_reader@sina.com"; 
    	String password = "spring_alex"; 
    	String from = "alex_reader@sina.com"; 
    	String to = "hua.yang@springdesign.com,howard@xfok.net"; 
    	String subject = "Java Mail Subject!"; 
    	String body = "Java Mail Body!";
    	MailSender sender=new JMailSender(smtp,username,password);
    	try{
    		sender.send(from, to, subject, body);
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
    private void testGMailSender(){
    	String username = "service@xfok.net"; 
    	String password = "xfok_service"; 
    	String from = "howard@xfok.net"; 
    	String to = "hua.yang@springdesign.com,howard@xfok.net"; 
    	String subject = "GMail Subject!"; 
    	String body = "GMail Body!";
    	MailSender sender=new GMailSender(username,password);
    	try{
    		sender.send(from, to, subject, body);
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
    private void testHttpClient(){
    	String url="http://www.xfok.net/ad.txt";
    	String charset="gb2312";
    	HttpClient client=new HttpClient();
    	String html=client.request(url, charset);
    	tv.setText(html);
    }
    
    private void testFilter(){
    	String url="http://finance.ifeng.com/stock/zqyw/20100830/2565985.shtml";
    	String charset="utf-8";
    	HttpClient client=new HttpClient();
    	String html=client.request(url, charset);
    	Rule rule=new Rule();
    	rule.setStartStr("id=artical_real>");
    	rule.setEndStr("<p>更多数据,请访问<a");
    	html=Filter.filter(html, rule);
    	wv.loadDataWithBaseURL(null, html, "text/html", charset, null);
    }
    
    private void testDB(){
    	Intent intent=new Intent(this,Demo.class);
    	startActivity(intent);
    }
    
    private void testRotateCube(){
    	Intent intent=new Intent(this,Rotate3dImage.class);
    	startActivity(intent);
    }
    
    private void testDict(){
    	Intent intent=new Intent(this,Dict.class);
    	startActivity(intent);
    }
    
    private class Animation extends Rotate3D{  
    	private static final String TAG="Animation";
		public Animation(ViewGroup mContainer) {
			super(mContainer);
		}

		protected void process() {
			// TODO Auto-generated method stub	
			Log.i(TAG, "Rotate 3D");
		}
    	
    }

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bt_3d:
			mAnim.applyRotation(0, 180);
			break;
		case R.id.bt_db:
			testDB();
			break;
		case R.id.bt_cube:
			testRotateCube();
			break;
		case R.id.bt_dict:
			testDict();
			break;
		default:
			break;
		}
		
	}

}